import api from '@/api';

export default {
  namespace: 'accountmanage',

  state: {
    newAccounts: {
      list: [],
      pagination: {},
    },
    auditAccounts: {
      list: [],
      pagination: {},
    },
    currentNewAccount: {},
    transType: [],
    selectedCus: {
      cus: [],
      status: '',
    },
    selectedAuitCus: {
      cus: [],
      status: '',
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.accountmanage.newAccount.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveNewAccounts',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *fetchAudit({ payload }, { call, put }) {
      const response = yield call(api.accountmanage.auditAccount.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveAuditAccounts',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },

    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.accountmanage.newAccount.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response;
    },
    *fetchTransType({ payload }, { call, put }) {
      const response = yield call(api.accountmanage.newAccount.queryTransType, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveTransType',
          payload: response.data,
        });
      }
      return response;
    },
    *add({ payload }, { call }) {
      return yield call(api.accountmanage.newAccount.creat, payload);
    },
    *submitAudit({ payload }, { call }) {
      return yield call(api.accountmanage.auditAccount.audit, payload);
    },
  },

  reducers: {
    saveNewAccounts(state, { payload }) {
      return {
        ...state,
        newAccounts: payload,
      };
    },
    saveAuditAccounts(state, { payload }) {
      return {
        ...state,
        auditAccounts: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentNewAccount: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentNewAccount: {},
      };
    },

    saveTransType(state, { payload }) {
      return {
        ...state,
        transType: payload,
      };
    },
    saveSelectedCus(state, { payload }) {
      return {
        ...state,
        selectedCus: payload,
      };
    },
    saveSelectedAuitCus(state, { payload }) {
      return {
        ...state,
        selectedAuitCus: payload,
      };
    },
  },
};
